package com.treetao.union.db.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treetao.union.db.mapper.CommonSettingMapper;
import com.treetao.union.db.model.CommonSetting;
import com.treetao.union.db.service.CommonSettingService;

/**
 * <p>
 * CommonSetting 服务实现类
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommonSettingServiceImpl extends ServiceImpl<CommonSettingMapper, CommonSetting>
		implements CommonSettingService {

	@Cacheable(value = "commonSetting", key = "#type", unless = "#result == null")
	@Override
	public CommonSetting getCommonSetting(String type) {
		QueryWrapper<CommonSetting> q = new QueryWrapper<CommonSetting>();
		q.eq("type", type);
		CommonSetting setting = getOne(q);
		if(setting!=null) {
			setting.setId(null);
			setting.setExts(null);
			setting.setDelFlag(null);
		}
		return setting;
	}

	@CacheEvict(value = "commonSetting", key = "#setting.type", beforeInvocation = false,condition = "#setting.type ne null ")
	@Override
	public boolean updateCommonSetting(CommonSetting setting) {
		QueryWrapper<CommonSetting> q = new QueryWrapper<CommonSetting>();
		q.eq("type", setting.getType());
		
		CommonSetting db = getCommonSetting(setting.getType());
		if(db==null) {
			return save(setting);
		}
		
		return update(setting, q);
	}

}
